/*
 *   功能： gulp任务执行的默认路径
 *   主要插件：
 *       1. gulp                      ---        gulp基础组件
 *       2. del                       ---        删除文件
 *       3. minimist                  ---        解析参数选项变为json对象
 *       4. browser-sync              ---        搭建web服务器
 *
 * */

// node、npm版本检测
require('./gulp/check-versions')()

var gulp = require('gulp'),        // 基础库
  watch = require('gulp-watch'),
  minimist = require('minimist'),
  del = require('del'),
  runSequence = require('run-sequence'),
  connect = require('gulp-connect'),
  proxy = require('http-proxy-middleware'),
  opn = require('opn')

var knownOptions = {
  string: 'env',
  default: {
    env: 'production'
  }
}

// 引入配置参数
var options = minimist(process.argv.slice(2), knownOptions)
var config = require('./gulp/config.js')(options)

/* 引入独立任务模块, 建立多个任务 */
var taskBasePath = './gulp/tasks/'
var htmlTask = require(taskBasePath + 'htmlTask'),
  cssTask = require(taskBasePath + 'cssTask'),
  scssTask = require(taskBasePath + 'scssTask'),
  scriptsTask = require(taskBasePath + 'scriptsTask'),
  imagesTask = require(taskBasePath + 'imagesTask'),
  spriteTask = require(taskBasePath + 'spriteTask'),
  othersTask = require(taskBasePath + 'othersTask'),
  pugTask = require(taskBasePath + 'pugTask')
tsTask = require(taskBasePath + 'tsTask')

gulp.task('html', function () {
  return htmlTask.html({}, config, connect)
})
gulp.task('script', function () {
  return scriptsTask.script({}, config, connect)
})
gulp.task('sprite', function () {
  return spriteTask.sprite(config, connect)
})
gulp.task('images', function () {
  return imagesTask.images({}, config, connect)
})
gulp.task('scss', function () {
  return scssTask.scss(config, connect)
})
gulp.task('css', function () {
  return cssTask.css({}, config, connect)
})
gulp.task('htmlInclude', function () {
  return htmlTask.htmlInclude({}, config, connect)
})
gulp.task('htmlIncludeWatch', ['htmlInclude'], function (done) {
  gulp.src(config.dist + '/**/*.html')
    .pipe(connect.reload())
  done()
})
/* 手机端雪碧任务 */
gulp.task('phoneSprite', function () {
  return spriteTask.phoneSprite(config, connect)
})
gulp.task('pug', function () {
  return pugTask.pugBuild({}, config, connect)
})
gulp.task('ts', function () {
  return tsTask.tsBuild({}, config, connect)
})
gulp.task('others', function () {
  return othersTask.others({}, config, connect)
})

/* 手机、web端的响应式页面服务器 */
gulp.task('phoneServe', function () {
  let clean = options.clean !== 'false' ? ['clean'] : ['pug']
  runSequence(clean, ['sprite', 'phoneSprite'], ['pug', 'ts', 'others'], ['scss', 'images'], ['script', 'html', 'css'], function () {
    /* 服务器 */
    connect.server({
      root: options.resource ? [config.dist, config.src] : [config.dist],
      port: config.connect.port,
      host: '0.0.0.0',
      livereload: true,
      middleware: function (connect, opt) {    /* 通过 http-proxy-middleware 设置代理 */
        let ret = []
        if (options.proxy) {
          let proxyConfig = eval('(' + options.proxy + ')')
          if (/^\{.*\}$/gi.test(options.proxy)) { // 只有一个代理
            ret.push(proxy(proxyConfig.path, {
              target: proxyConfig.target,
              changeOrigin: true
            }))
          } else if (/^\[.*\]$/gi.test(options.proxy)) { // 数组
            proxyConfig.forEach(function (pConfig) {
              ret.push(proxy(pConfig.path, {
                target: pConfig.target,
                changeOrigin: true
              }))
            })
          }
          return ret
        } else {
          return []
        }
      }
    })

    watch(config.htmlConfig.src, function (options) {htmlTask.html({type: options.event, path: options.history}, config, connect)})   // 监控html源文件
    watch(config.htmlConfig.ignore, ['htmlIncludeWatch']) // include模块内容监控
    watch(config.spriteConfig.spriteSrc + '/**/*.*', ['sprite']) // 监控雪碧图任务
    watch(config.phoneSprite.spriteSrc + '/**/*.*', ['phoneSprite'])  // 监控手机雪碧图任务
    watch(config.cssConfig.src, function (options) {cssTask.css({type: options.event, path: options.history}, config, connect) })  // 监控css样式源文件
    watch(config.scssConfig.scssSrc + '/**/*.scss', function () {scssTask.scss(config, connect) }) // 监控scss样式源文件
    watch(config.imagesConfig.src, function (options) { imagesTask.images({type: options.event, path: options.history}, config, connect) }) // 监控所有图片
    watch(config.src + '/js/**/**', function (options) {scriptsTask.script({type: options.event, path: options.history}, config, connect) }) // 监控js源文件
    watch(config.pugConfig.src, function (options) {pugTask.pugBuild({type: options.event, path: options.history}, config, connect)})   // 监控pug源文件
    watch(config.tsConfig.src, function (options) {tsTask.tsBuild({type: options.event, path: options.history}, config, connect)})   // 监控ts 文件
    watch([config.pugConfig.ignore + '/**/**'], function () {pugTask.pugBuild({}, config, connect)})   // 监控pug模板文件
    watch(config.dist + '/css/main/images/**/**').on('change', function () {connect.reload()})// 监控目标文件夹下雪碧变化
    watch(config.others.src, function (options) {othersTask.others({type: options.event, path: options.history}, config, connect)})
    watch(config.others.ignore, function () {
      gulp.src(config.dist + '/**/*.html').pipe(connect.reload())
    })
    gulp.run('sprite')//解决雪碧图第一次生成错误问题
    gulp.run('phoneSprite')//解决雪碧图第一次生成错误问题

    /* 浏览器自动打开开始页面 */
    opn('http://localhost:' + config.connect.port + '/index.html')
  })
})

//删除temp和dist下的所有文件 等同于 gulp.task('clean',function(){require('del')(['.tmp','dist'])});
gulp.task('clean', del.bind(null, config.cleanConfig.src, {
  force: true
}))

/* iChoice雪碧图任务 */
gulp.task('iChoice', function () {
  spriteTask.iChoice(config)
})
